#include <stdio.h>
#include <stdlib.h>
#define boardMax 10
void menu();
void menu1();
void printfnowBoard(char nowboard[boardMax][boardMax]);
void printfCoordinate(int *p, int *q, int *p1, int *q1);
void continueJudge(char nowboard2[boardMax][boardMax]);
char moveChessman(int x, int y, int x1, int y1, char nowboard[boardMax][boardMax]);
int readFile(char nowboard[boardMax][boardMax]);
void writeFile(char nowboard3[boardMax][boardMax]);
// extern char filename[];
void existFile(char nowboard1[boardMax][boardMax], char board1[boardMax][boardMax]);
void notExistFile(char nowboard1[boardMax][boardMax], char board1[boardMax][boardMax]);
int moveRule(int x, int y, int x1, int y1, char nowboard2[boardMax][boardMax], char board2[boardMax][boardMax]);

void menu() // 有存档的菜单
{
    printf("   双人象棋\n");
    printf("----------------\n");
    printf("  新游戏-----1\n");
    printf("  继续游戏---2\n");
    printf("  退出-------0\n");
    printf("----------------\n");
    return;
}
void menu1() // 无存档的菜单
{
    printf("   双人象棋\n");
    printf("----------------\n");
    printf("  新游戏-----1\n");
    printf("  退出-------0\n");
    printf("----------------\n");
    return;
}
void printfnowBoard(char nowboard[boardMax][boardMax]) // 打印实时棋盘
{
    for (int i = 0; i < boardMax; i++)
    {
        for (int j = 0; j < boardMax - 1; j++)
        {
            printf("%c  ", nowboard[i][j]);
        }
        printf("  ");
        for (int j = 0; j < boardMax - 1; j++)
        {
            printf("(%d,%d) ", j, i);
        }
        printf("\n");
    }
    printf("\n兵(G、g)、炮(F、f)、车(E、e)、马(D、d)、象(C、c)、士(B、b)、将(A、a)\n");
    return;
}
void printfCoordinate(int *p, int *q, int *p1, int *q1) // 输入坐标
{
    printf("\n输入要移动棋子的坐标:");
    printf("\n横坐标：");
    scanf_s("%d", p);
    printf("纵坐标：");
    scanf_s("%d", q);
    printf("\n输入移动后的坐标：");
    printf("\n横坐标：");
    scanf_s("%d", p1);
    printf("纵坐标：");
    scanf_s("%d", q1);
    printf("\n");
    return;
}

void continueJudge(char nowboard2[boardMax][boardMax]) // 判断是否继续游戏
{
    int judge;
    printf("继续游戏输入任意数字,退出输入0:\n");
    scanf_s("%d", &judge);
    if (judge == 0)
    {
        writeFile(nowboard2);
        exit(0);
    }
    else
    {
        return;
    }
}
char moveChessman(int x, int y, int x1, int y1, char nowboard[boardMax][boardMax]) // 移动棋子
{
    char savePrice = nowboard[y1][x1];
    nowboard[y1][x1] = nowboard[y][x];
    nowboard[y][x] = '*';
    return savePrice;
}
int readFile(char nowboard[boardMax][boardMax]) // 读取存档
{
    FILE *fp = fopen("boarddata.dat", "rb");
    if (fp == NULL)
    {
        printf("文件不存在！");
    }
    fseek(fp, 0, SEEK_END);     // 移动文件指针到文件末尾
    long long size = ftell(fp); // 获取文件大小
    rewind(fp);                 // 将文件指针重置到文件开头
    if (size == 0)
    {
        printf("存档不存在！\n");
        return 0;
    }
    for (int i = 0; i < boardMax; i++)
    {
        for (int j = 0; j < boardMax; j++)
        {
            fread(&nowboard[i][j], sizeof(char) * boardMax * boardMax, 1, fp);
        }
    }
    fclose(fp);
    return 1;
}
void writeFile(char nowboard3[boardMax][boardMax]) // 写入存档
{
    FILE *fp = fopen("boarddata.dat", "wb");
    for (int i = 0; i < boardMax; i++)
    {
        for (int j = 0; j < boardMax; j++)
        {
            fwrite(&nowboard3[i][j], sizeof(char) * boardMax * boardMax, 1, fp);
        }
    }
    fclose(fp);
    return;
}

void existFile(char nowboard1[boardMax][boardMax], char board1[boardMax][boardMax]) // 有存档的情况
{
    int n;
    int x, y, x1, y1; // 移动棋子前的坐标和移动棋子后的坐标
    while (1)
    {
        char savePrice = 'z'; // 保存被吃掉的棋子
        menu();
        printf("请选择：");
        scanf_s("%d", &n);
        switch (n)
        {
        case 1:
        {
            int f = 1;
            for (int i = 0; i < boardMax; i++)
            {
                for (int j = 0; j < boardMax; j++)
                {
                    nowboard1[i][j] = board1[i][j];
                }
            }
            while (1)
            {
                if (savePrice == 'A' || savePrice == 'a')
                {
                    break;
                }
                printfnowBoard(nowboard1); // 打印棋盘
                continueJudge(nowboard1);  // 判断是否继续游戏
                printfCoordinate(&x, &y, &x1, &y1);
                int judge_possibility = moveRule(x, y, x1, y1, nowboard1, board1);
                if (judge_possibility == 0) // 判断是否可以移动
                {
                    printf("移动失败!请按照规则移动棋子!\n\n");
                }
                else
                {
                    savePrice = moveChessman(x, y, x1, y1, nowboard1);
                }
            }
            if (savePrice == 'A' || savePrice == 'a') // 判断是否胜利
            {
                printf("游戏结束！请退出后重新打开游戏在进行游玩。\n");
                FILE *fp = fopen("boarddata.dat", "w");
                fclose(fp);
                system("pause");
                return;
            }
            break;
        }
        case 2:
        {
            while (1)
            {
                if (savePrice == 'A' || savePrice == 'a') // 判断是否胜利
                {
                    break;
                }
                printfnowBoard(nowboard1); // 打印实时棋盘
                continueJudge(nowboard1);  // 判断是否继续游戏
                printfCoordinate(&x, &y, &x1, &y1); // 输入棋子坐标
                int judge_possibility = moveRule(x, y, x1, y1, nowboard1, board1); // 判断是否可以移动
                if (judge_possibility == 0)
                {
                    printf("移动失败!请按照规则移动棋子!\n\n");
                }
                else
                {
                    savePrice = moveChessman(x, y, x1, y1, nowboard1);
                }
            }
            if (savePrice == 'A' || savePrice == 'a')
            {
                printf("游戏结束！请退出后重新打开游戏在进行游玩。\n");
                FILE *fp = fopen("boarddata.dat", "w"); // 清空存档
                fclose(fp);
                system("pause");
                return;
            }
            break;
        }
        case 0:
        {
            FILE *fp = fopen("boarddata.dat", "w"); // 清空存档
            fclose(fp);
            printf("退出中.......\n");
            exit(0);
        }
        }
    }
}
void notExistFile(char nowboard1[boardMax][boardMax], char board1[boardMax][boardMax]) // 没有存档的情况
{
    int n;
    int x, y, x1, y1; // 移动棋子前的坐标和移动棋子后的坐标
    while (1)
    {
        char savePrice = 'z'; // 保存被吃掉的棋子
        menu1();
        printf("请选择：");
        scanf_s("%d", &n);
        switch (n)
        {
        case 1:
        {
            for (int i = 0; i < boardMax; i++)
            {
                for (int j = 0; j < boardMax; j++)
                {
                    nowboard1[i][j] = board1[i][j];
                }
            }
            while (1)
            {
                if (savePrice == 'A' || savePrice == 'a')
                {
                    break;
                }
                printfnowBoard(nowboard1);
                continueJudge(nowboard1);
                printfCoordinate(&x, &y, &x1, &y1);
                int judge_possibility = moveRule(x, y, x1, y1, nowboard1, board1);
                if (judge_possibility == 0)
                {
                    printf("移动失败!请按照规则移动棋子!\n\n");
                }
                else
                {
                    savePrice = moveChessman(x, y, x1, y1, nowboard1);
                }
            }
            if (savePrice == 'A' || savePrice == 'a')
            {
                printf("游戏结束！请退出后重新打开游戏在进行游玩。\n");
                FILE *fp = fopen("boarddata.dat", "w");
                fclose(fp);
                system("pause");
                return;
            }
            break;
        }
        case 0:
        {
            FILE *fp = fopen("boarddata.dat", "w"); // 清空存档
            fclose(fp);
            printf("退出中.......\n");
            exit(0);
        }
        }
    }
}
int moveRule(int x, int y, int x1, int y1, char nowboard2[boardMax][boardMax], char board2[boardMax][boardMax]) // 移动规则
{
    if (x > 8 || y > 9 || x1 > 8 || y1 > 9 || x < 0 || y < 0 || x1 < 0 || y1 < 0) // 判断是否在棋盘内
    {
        return 0;
    }
    if (nowboard2[y][x] >= 65 && nowboard2[y][x] <= 71 && nowboard2[y1][x1] >= 65 && nowboard2[y1][x1] <= 71 || nowboard2[y][x] >= 97 && board2[y][x] <= 103 && board2[y1][x1] >= 97 && board2[y1][x1] <= 103) // 判断是否为己方棋子
    {
        return 0;
    }
    if (x == x1 && y == y1)
    {
        return 0;
    }
    if (nowboard2[y][x] == 'E') // 判断是否是E（車）
    {
        if (x != x1 && y == y1)
        {
            if (x < x1)
            {
                for (int i = x + 1; i < x1; i++)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        return 0;
                    }
                }
                return 1;
            }
            else
            {
                for (int i = x - 1; i > x1; i--)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        return 0;
                    }
                }
                return 1;
            }
        }
        else if (x == x1 && y != y1)
        {
            if (y < y1)
            {
                for (int i = y + 1; i < y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        return 0;
                    }
                }
                return 1;
            }
            else
            {
                for (int i = y - 1; i > y1; i--)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        return 0;
                    }
                }
                return 1;
            }
        }
    }
    else if (nowboard2[y][x] == 'e') // 判断是否是e（車）
    {
        if (x != x1 && y == y1)
        {
            if (x < x1)
            {
                for (int i = x + 1; i < x1; i++)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        return 0;
                    }
                }
            }
            else
            {
                for (int i = x - 1; i > x1; i--)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        return 0;
                    }
                }
            }
        }
        else if (x == x1 && y != y1)
        {
            if (y < y1)
            {
                for (int i = y + 1; i < y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            else
            {
                for (int i = y - 1; i > y1; i--)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
        }
    }
    else if (nowboard2[y][x] == 'D') // 判断是否是D（马）
    {
        if (x1 == (x - 2) && y1 == (y - 1) && nowboard2[y][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y + 1) && nowboard2[y][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y - 1) && nowboard2[y][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y + 1) && nowboard2[y][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 1) && y1 == (y + 2) && nowboard2[y + 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 1) && y1 == (y - 2) && nowboard2[y - 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 1) && y1 == (y + 2) && nowboard2[y + 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 1) && y1 == (y - 2) && nowboard2[y - 1][x] == '*')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    if (nowboard2[x][y] == 'd') // 判断是否为d（马）
    {
        if (x1 == (x - 2) && y1 == (y - 1) && nowboard2[y][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y + 1) && nowboard2[y][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y - 1) && nowboard2[y][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y + 1) && nowboard2[y][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 1) && y1 == (y + 2) && nowboard2[y + 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 1) && y1 == (y - 2) && nowboard2[y - 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 1) && y1 == (y + 2) && nowboard2[y + 1][x] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 1) && y1 == (y - 2) && nowboard2[y - 1][x] == '*')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if (y1 >= 0 && y1 <= 4 && nowboard2[y][x] == 'C') // 判断是否为C（象）和限制象不能过河
    {
        if (x1 == (x + 2) && y1 == (y - 2) && nowboard2[y - 1][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y + 2) && nowboard2[y + 1][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y + 2) && nowboard2[y + 1][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y - 2) && nowboard2[y - 1][x - 1] == '*')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if (y1 >= 5 && y1 <= 9 && nowboard2[x][y] == 'c') // 判断是否为c（相）
    {
        if (x1 == (x + 2) && y1 == (y - 2) && nowboard2[y - 1][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x + 2) && y1 == (y + 2) && nowboard2[y + 1][x + 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y + 2) && nowboard2[y + 1][x - 1] == '*')
        {
            return 1;
        }
        else if (x1 == (x - 2) && y1 == (y - 2) && nowboard2[y - 1][x - 1] == '*')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if (nowboard2[y][x] == 'B') // 判断是否为B（士）
    {
        if (x1 == 3 && y1 == 0 || x1 == 5 && y1 == 0 || x1 == 3 && y1 == 2 || x1 == 5 && y1 == 2 || x1 == 4 && y1 == 1)
        {
            if (x1 == 4 && y1 == 1 && (x == 3 && y == 0 || x == 5 && y == 0 || x == 3 && y == 2 || x == 5 && y == 2))
            {
                return 1;
            }
            else if (x == 4 && y == 1 && (x1 == 3 && y1 == 0 || x1 == 5 && y1 == 0 || x1 == 3 && y1 == 2 || x1 == 5 && y1 == 2))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    else if (nowboard2[y][x] == 'b') // 判断是否为b（士）
    {
        if (x1 == 3 && y1 == 7 || x1 == 5 && y1 == 7 || x1 == 3 && y1 == 9 || x1 == 5 && y1 == 9 || x1 == 4 && y1 == 8)
        {
            if (x1 == 4 && y1 == 8 && (x == 3 && y == 7 || x == 5 && y == 7 || x == 3 && y == 9 || x == 5 && y == 9))
            {
                return 1;
            }
            else if (x == 4 && y == 8 && (x1 == 3 && y1 == 7 || x1 == 5 && y1 == 7 || x1 == 3 && y1 == 9 || x1 == 5 && y1 == 9))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    else if (nowboard2[y][x] == 'A') // 判断是否为A（将）
    {
        if (x1 >= 3 && x1 <= 5 && y1 >= 0 && y1 <= 2)
        {
            if (x == x1)
            {
                int num = 0;
                for (int i = y + 1; i < y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num == 1;
                    }
                }
                if (num == 0)
                {
                    return 1;
                }
            }
            if (x == x1 && (y1 == (y + 1) || y1 == (y - 1)))
            {
                return 1;
            }
            else if (y == y1 && (x1 == (x + 1) || x1 == (x - 1)))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    else if (nowboard2[y][x] == 'a') // 判断是否为a（帅）
    {
        if (x >= 3 && x <= 5 && y >= 7 && y <= 9)
        {
            if (x == x1)
            {
                int num = 0;
                for (int i = y - 1; i > y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num == 1;
                    }
                }
                if (num == 0)
                {
                    return 1;
                }
            }
            if (x == x1 && (y1 == (y + 1) || y1 == (y - 1)))
            {
                return 1;
            }
            else if (y == y1 && (x1 == (x + 1) || x1 == (x - 1)))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    else if (nowboard2[y][x] == 'F') // 判断是否为F（炮）
    {
        int num = 0;
        if (x == x1 && y != y1)
        {
            if (y < y1)
            {
                num = 0;
                for (int i = (y + 1); i < y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
            if (y > y1)
            {
                num = 0;
                for (int i = (y - 1); i > y1; i--)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x1] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
        }
        if (x != x1 && y == y1)
        {
            if (x < x1)
            {
                num = 0;
                for (int i = (x + 1); i < x1; i++)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
            if (x > x1)
            {
                num = 0;
                for (int i = (x - 1); i > x1; i--)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x1] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
        }
    }
    else if (nowboard2[y][x] == 'f') // 判断是否为f（炮）
    {
        int num = 0;
        if (x == x1 && y != y1)
        {
            if (y < y1)
            {
                num = 0;
                for (int i = (y + 1); i < y1; i++)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
            if (y > y1)
            {
                num = 0;
                for (int i = (y - 1); i > y1; i--)
                {
                    if (nowboard2[i][x] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x1] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
        }
        if (x != x1 && y == y1)
        {
            if (x < x1)
            {
                num = 0;
                for (int i = (x + 1); i < x1; i++)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
            if (x > x1)
            {
                num = 0;
                for (int i = (x - 1); i > x1; i--)
                {
                    if (nowboard2[y][i] != '*')
                    {
                        num++;
                    }
                }
                if (num == 0 && nowboard2[y1][x1] == '*' || num == 1 && nowboard2[y1][x1] != '*')
                {
                    return 1;
                }
                else if (num == 0 && nowboard2[y1][x1] != '*' || num == 1 && nowboard2[y1][x1] == '*' || num > 1)
                {
                    return 0;
                }
            }
        }
    }
    else if (nowboard2[y][x] == 'G') // 判断是否为G（兵）
    {
        if (y <= 4)
        {
            if (x != x1 || y > y1 || y1 - y > 1)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        if (y >= 5)
        {
            if (y > y1 || x1 - x > 1 || x - x1 > 1)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
    else if (nowboard2[y][x] == 'g') // 判断是否为g（兵）
    {
        if (y <= 5)
        {
            if (x == x1 && y != y1)
            {
                if (y1 < y || y1 - y > 1)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                return 0;
            }
        }
        if (y >= 5)
        {
            if (x == x1 && y != y1)
            {
                if (y1 < y || y1 - y > 1)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else if (x != x1 && y == y1)
            {
                if (x - x1 > 1 || x1 - x > 1)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
        }
    }
    else
    {
        return 0;
    }
}